package org.example.chinesedays.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.chinesedays.utils.getDaysAndWriteFile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.time.LocalDate;


@Component
@Slf4j
@RequiredArgsConstructor
public class getHolidaysTask {
    private final getDaysAndWriteFile daysAndWriteFile;

    // 定时任务，每天12点半执行一次
    @Scheduled(cron = "0 30 12 * * ? ")
    public void getHolidays() {
        // 获取当前年份
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        if (now.getMonth().getValue()>10) {
            year+=1;
        }
        log.info("开始获取 {} 年的节假日信息...", year);
        try {
            daysAndWriteFile.getDays(year);
        } catch (IOException | InterruptedException e) {
            log.error("获取节假日信息时发生异常：", e);
        }
    }
}
